package cn.agiledata.bank.transaction.action;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.uns.upms.util.excel.DocumentCompose;
import com.uns.upms.util.excel.DocumentComposeFactoryBean;
import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.DateUtil;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.BalanceRecord;
import cn.agiledata.bank.myspace.service.IMySpaceService;
import cn.agiledata.bank.transaction.dao.GateWayTransQueryPageList;
import cn.agiledata.bank.transaction.form.GateWayTransQueryForm;
import cn.agiledata.bank.transaction.logic.GateWayTransQueryBean;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.action.IPBBaseDynaForm;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

public class GateWayTransAction extends IPBBaseAction {

	private AccountTemplateInter getAccountTemplate() {
        return (AccountTemplateInter) IPBAppFactory
                .getObj(ForeDict.ACCOUNT_TEMPLATE);
    }
	protected IMySpaceService getMySpaceService() throws IPBException {
		return (IMySpaceService) IPBAppFactory.getObj(IMySpaceService.MGR_ID);
	}
	/**
	 * 取得当前账户
	 * 
	 * @param request
	 * @return
	 */
    private Account getCurrentAccount(HttpServletRequest request) {
        long accountId = WeatherUtil.getAccountIdFromSession(request);
        return getAccountTemplate().getAccount(String.valueOf(accountId));
    }
    /**
	 * 取得网关商户结算记录列表
	 * 
	 * @param request
	 * @return
	 */
    public final ActionForward gateWayTransQuery(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	IPBBaseDynaForm dynaForm = (IPBBaseDynaForm) form;
		int startPage = 1;
		
		if (dynaForm.get("startPage") != null
				&& !dynaForm.get("startPage").equals("")) {
			startPage = Integer.parseInt((String) dynaForm.get("startPage"));
		}
    	Account account = getCurrentAccount(request);
    	GateWayTransQueryBean queryBean = new GateWayTransQueryBean();
    	queryBean.setAccount(account);
    	queryBean.setStartPage(String.valueOf(startPage));
    	if (dynaForm.get("beginDate") != null && !"".equals(dynaForm.get("beginDate"))) {
    		queryBean.setBeginDate(DateUtil.string2Date(dynaForm.get("beginDate").toString()));
    	}
    	if (dynaForm.get("endDate") != null && !"".equals(dynaForm.get("endDate"))) {
    		queryBean.setEndDate(DateUtil.string2Date(dynaForm.get("endDate").toString() + " 23:59:59"));
    	}
    	GateWayTransQueryPageList gateWayPage = new GateWayTransQueryPageList(queryBean);
    	gateWayPage.setStartPage(String.valueOf(startPage));
    	gateWayPage.setPageRecord(String.valueOf(Constant.PAGESIZE));
    	PageBean pb = gateWayPage.getAccountListPageBean();

    	request.setAttribute("result", pb);
    	return mapping.findForward("list");
    }
    
    public ActionForward exportGateWayTrans(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    	IPBBaseDynaForm dynaForm = (IPBBaseDynaForm) form;
    	Account account = getCurrentAccount(request);
    	GateWayTransQueryBean queryBean = new GateWayTransQueryBean();
    	queryBean.setAccount(account);
    	if (dynaForm.get("beginDate") != null && !"".equals(dynaForm.get("beginDate"))) {
    		queryBean.setBeginDate(DateUtil.string2Date(dynaForm.get("beginDate").toString()));
    	}
    	if (dynaForm.get("endDate") != null && !"".equals(dynaForm.get("endDate"))) {
    		queryBean.setEndDate(DateUtil.string2Date(dynaForm.get("endDate").toString() + " 23:59:59"));
    	}
    	Map mapExcel = new HashMap();
    	GateWayTransQueryPageList gateWayPage = new GateWayTransQueryPageList(queryBean);
    	gateWayPage.setStartPage(null);
    	gateWayPage.setPageRecord(null);
    	PageBean pb = gateWayPage.getAccountListPageBean();
    	List data = pb.getData();
    	List listOut = new ArrayList();
    	Map tileMap = new HashMap();
		tileMap.put("startdate", "结算起始时间");
		tileMap.put("enddate", "结算截至时间");
		tileMap.put("amount", "结算金额");
		tileMap.put("createtime", "结算时间");
		listOut.add(tileMap);
		if (data != null && data.size() > 0) {
			for (int i = 0; i < data.size(); i++) {
				BalanceRecord balRecord = (BalanceRecord) data.get(i);
				Map mapLine = new HashMap();
				if (balRecord.getStartDate() != null){
					mapLine.put("startdate", DateUtil.date2String(balRecord.getStartDate(), DateUtil.DATE_FORMAT_DATE_TIME));
				}
				if (balRecord.getEndDate() != null){
					mapLine.put("enddate", DateUtil.date2String(balRecord.getEndDate(), DateUtil.DATE_FORMAT_DATE_TIME));
				}
				mapLine.put("amount", balRecord.getAmount());
				if (balRecord.getCreateTime() != null){
					mapLine.put("createtime", DateUtil.date2String(balRecord.getCreateTime(), DateUtil.DATE_FORMAT_DATE_TIME));
				}
				listOut.add(mapLine);
			}
		}

		mapExcel.put(DocumentComposeFactoryBean.VALUE_LINE, listOut);
		
		OutputStream os = response.getOutputStream();
		response.setContentType("application/vnd.ms-excel");
		//调用已经定义好的模板.
		DocumentCompose compose = DocumentComposeFactoryBean.xlsComposeFactory(
				"eportgatewaybalance", os, mapExcel);
		//导出文件到本地
		os = compose.composeXlsStream();
		os.flush();
		os.close();
		response.flushBuffer();
		
    	return null;
    }
    
    /**
	 * 账户历史查询列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward accountQueryHis(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws IPBException, Exception {
		IPBBaseDynaForm dynaForm = (IPBBaseDynaForm) form;
		int startPage = 1;
		
		if (dynaForm.get("startPage") != null
				&& !dynaForm.get("startPage").equals("")) {
			startPage = Integer.parseInt((String) dynaForm.get("startPage"));
		}
		String viewDetail = (String) dynaForm.get("viewDetail");
		String[] dates = viewDetail.split(",");
		String beginDate = dates[0];
		String endDate = dates[1];
		//清空结算的查询条件
		dynaForm.getMap().put("beginDate", null);
		dynaForm.getMap().put("endDate", null);
		//获取某一结算的开始截至日期
		dynaForm.getMap().put("gateWayBeginDate", beginDate);
		dynaForm.getMap().put("gateWayEndDate", endDate);
		dynaForm.getMap().put("transStatus", Constant.ES_TRANS_STATUS_FINISH);
		dynaForm.getMap().put("returnTrans", Constant.TRANS_STATUS_REFUNDSUCC);
		List typeList = new ArrayList();
		typeList.add(new Long(Constant.ACTION_TYPE_C));//3.账户间交易
		typeList.add(new Long(Constant.ACTION_TYPE_E));//5.冲正
		typeList.add(new Long(Constant.ACTION_TYPE_F));//6.银行卡付款给对方账户
		typeList.add(new Long(Constant.ACTION_TYPE_L));//12.电话银行付款给对方账户
		typeList.add(new Long(Constant.ACTION_TYPE_P));//18.B2B网上银行付款
		typeList.add(new Long(Constant.ACTION_TYPE_Q));//19.卡付款
		typeList.add(new Long(Constant.ACTION_TYPE_R));//20.退款
		dynaForm.getMap().put("typeList", typeList);

		String accountId = String.valueOf(WeatherUtil
				.getAccountIdFromSession(request));
		String accountName = WeatherUtil.getAccountNameFromSession(request);
		dynaForm.getMap().put("accountID", accountId);
		if (accountName != null) {
			dynaForm.getMap().put("accountName", accountName);
		}

		PageBean pb = getMySpaceService().getAccountHisListPageBean(
				dynaForm.getMap(), startPage, new Long(20).intValue());

		request.setAttribute("pagination", pb);
		request.setAttribute("accountID", accountId);
		Account account = getMySpaceService().getAccount(accountId);
		request.setAttribute("account", account);
		
		return mapping.findForward("queryHisList");
	}
}
